#!/bin/bash
# EbbFlow AWS Test Script

set -e

echo "=== EbbFlow AWS Test ==="

# Get instance IPs
IPS=$(aws ec2 describe-instances \
    --filters "Name=tag:Name,Values=ebbflow-node-*" \
    --query 'Reservations[*].Instances[*].PublicIpAddress' \
    --output text)

echo "Instance IPs: $IPS"

# Start servers on all nodes
for ip in $IPS; do
    echo "Starting server on $ip..."
    ssh -i ~/.ssh/ebbflow-key.pem ec2-user@$ip "cd ebbflow && ./bin/ebbflow_server 0 &"
done

# Wait for servers to start
sleep 10

# Start client on first node
FIRST_IP=$(echo $IPS | awk '{print $1}')
echo "Starting client on $FIRST_IP..."
ssh -i ~/.ssh/ebbflow-key.pem ec2-user@$FIRST_IP "cd ebbflow && ./bin/ebbflow_client 100 1 1000 test 1 &"

# Monitor performance
echo "Monitoring performance for 60 seconds..."
sleep 60

# Collect logs
echo "Collecting logs..."
mkdir -p logs
for ip in $IPS; do
    echo "Collecting logs from $ip..."
    scp -i ~/.ssh/ebbflow-key.pem ec2-user@$ip:ebbflow/logger/*.log logs/
done

echo "Test completed!"
echo "Logs collected in logs/ directory"
